// pages/address/address.js
var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		addresslist:[]
	},
	/* 选择收货地址 */
	selectaddres(e){
		let that=this;
		let selectaddres=e.currentTarget.dataset.selectaddres;
		let addresslist=that.data.addresslist;
		let index=e.currentTarget.dataset.index;
		if(selectaddres==undefined||selectaddres==false){
			for(let i=0;i<addresslist.length;i++){
				addresslist[index].selectaddres=false;
			}
			addresslist[index].selectaddres=true;
		}else{
			for(let i=0;i<addresslist.length;i++){
				addresslist[index].selectaddres=false;
			}
			addresslist[index].selectaddres=false;
		}
		that.setData({
			addresslist:addresslist
		})
		wx.setStorageSync('selectaddresd',addresslist[index]);
		let addresdirect=false;
		wx.setStorageSync('addresdirect', addresdirect);
		console.log('不跳转');
		wx.redirectTo({
			'url':'../confirmorder/confirmorder'
		})
		
	},
	/* 编辑地址 */
	editaddres(e) {
		let that=this;
		let id = e.currentTarget.dataset.id;
		let index = e.currentTarget.dataset.index;
		let addresslist=that.data.addresslist;
		wx.setStorageSync('editaddres',addresslist[index]);
		
		wx.navigateTo({
			'url': '../editaddres/editaddres?id=' + id
		})
	},
	/* 删除地址 */
	canseladdres(e) {
		let that=this;
		let userInfo = wx.getStorageSync('userInfo');
		let canselid = e.currentTarget.dataset.id;
		let index = e.currentTarget.dataset.index;
		let base_url = app.globalData.baseurl;
		let _url = base_url+'/address/delete?id='+canselid;
		wx.showModal({
			title: "提示",
			content: "确定删除此地址",
			showCancel: true,
			success:function(res){
				if (res.confirm) {
					that.confirmcanseladdres(userInfo,index,canselid)
				    console.log('用户点击确定')
				} else if (res.cancel) {
				    console.log('用户点击取消')
				}
			  
			}
		});
		
	},
	confirmcanseladdres(userInfo,index,canselid){
		let that=this;
		let index1=index;
		let base_url = app.globalData.baseurl;
		let _url = base_url+'/address/delete?id='+canselid;
		wx.request({
			url: _url,
			data: {},
			header: {
				'content-type': 'application/json', // 默认值
				'headerId':userInfo.openId,
				'headerValue':userInfo.headerValue 
			},
			success: function(res) {
				console.log(res);
				//return false;
				if (res.data.status==200) {
					/* 如果删除的地址和选择的收货地址一样则清除选择的地址 */
					let selectaddresd=wx.getStorageSync('selectaddresd');
					let canseladdresd=that.data.addresslist[index];
					if(canseladdresd.id==selectaddresd.id){
						wx.removeStorageSync('selectaddresd');
					}
					
					that.data.addresslist.splice(index,1);
					let address=that.data.addresslist;
					
					
					that.setData({
					  addresslist: address
					})
				}
			}
		})
	},
	/* 获取地址列表 */
	addresslist(e) {
		let userInfo = wx.getStorageSync('userInfo');
		let that = this;
		//收货地址列表
		let base_url = app.globalData.baseurl;
		let _url = base_url+'/address/query?openId='+userInfo.openId;
		wx.request({
			url: _url,
			data: {},
			header: {
				'content-type': 'application/json' ,// 默认值
				'headerId':userInfo.openId,
				'headerValue':userInfo.headerValue 
			},
			success: function(res) {
				console.log(res.data)
				let data=res.data;
				// for (var i = 0; i < res.data.index_slide.length; i++) {
				//   var item = res.data.data[i];
				//   item.imageurl = item.prex + item.path;
				//   res.data.data[i] = item;
				// }
				if (data.status==200) {
					
					let addresslist=data.data;
					//let regionarry=[];
					for(let i=0;i<addresslist.length;i++){
						addresslist[i].deliveryAddress=addresslist[i].deliveryAddress.split(',')
					}
					that.data.addresslist=addresslist;
					console.log(that.data.addresslist);
					if(addresslist.length==0){
						addresslist=false;
					}
					that.setData({
						addresslist: addresslist,
					})
				}
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let that = this;
		//that.addresslist();

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		let that = this;
		that.addresslist();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
